package cn.dyp.com.automaticcontrolclient.request;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 继承协议请求基类，初步解析服务器返回数据
 * 
 * @author asus
 */
public abstract class AgricultureRequest extends BaseRequest {
	// 存放服务器端的回应结果（成功或失败），基类只提供读取接口，具体内容由子类提供
	protected volatile boolean isSuccess = false;
	// 用户名
	protected String userName = "";

	/**
	 * 构造函数
	 * 
	 * @param userName
	 *            用户名
	 */
	public AgricultureRequest(String userName) {
		super();
		this.userName = userName;
	}

	/**
	 * 设置用户名
	 * 
	 * @param userName
	 *            用户名
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * 标记本次请求是否成功
	 * 
	 * @return 是否成功
	 */
	public boolean isSuccess() {
		return isSuccess;
	}

	/**
	 * JSON数据解析
	 * 
	 * @see cn.dyp.com.automaticcontrolclient.request.BaseRequest#onJasonParese(String)
	 */
	@Override
	protected void onJasonParese(String responseStr) {
		try {
			JSONObject jsonRespObj = new JSONObject(responseStr);
			if (jsonRespObj != null) {
				if (jsonRespObj.has("result")) {
					String result = jsonRespObj.getString("result");
					// “result”键值为"ok"则成功，否则失败
					if (result.equals("ok")) {
						isSuccess = true;
					} else {
						isSuccess = false;
					}
				}
			}
		} catch (JSONException e) {
			isSuccess = false;
			e.printStackTrace();
		}
	}
}
